package com.fly.demo.web.api;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.LongStream;

import javax.validation.Valid;

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fly.core.JsonResult;
import com.fly.demo.model.DataEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 
 * Swagger 注解演示
 * 
 * @author 00fly
 * @version [版本号, 2021年9月11日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Api(tags = "api-不支持跨域")
@RestController
@RequestMapping("/no-cors")
@ConditionalOnWebApplication
public class RestDataApi
{
    /**
     * @api {post} /no-cors/insert 新增
     * @apiGroup api-不支持跨域
     * @apiParam {String} id 主键
     * @apiParam {String} name 名称
     * @apiParam {String=F,M} sex 性别
     * @apiParam {Number=0,10,18,20} age 年齡
     * @apiParam {Number} salary 薪资
     * @apiParam {Number} game1 游戏1积分
     * @apiParam {Number} game2 游戏2积分
     */
    @Operation(summary = "新增")
    @PostMapping("/insert")
    public JsonResult<?> insert(@Valid DataEntity entity)
    {
        return JsonResult.success();
    }
    
    /**
     * @api {post} /no-cors/insertByJson Json传参新增
     * @apiGroup api-不支持跨域
     * @apiBody {String} id 主键
     * @apiBody {String} name 名称
     * @apiBody {String=F,M} sex 性别
     * @apiBody {Number=0,10,18,20} age 年齡
     * @apiBody {Number} salary 薪资
     * @apiBody {Number} game1 游戏1积分
     * @apiBody {Number} game2 游戏2积分
     */
    @Operation(summary = "Json传参新增")
    @PostMapping("/insertByJson")
    public JsonResult<?> insertByJson(@Valid @RequestBody DataEntity entity)
    {
        return JsonResult.success();
    }
    
     /**
     * @api {delete} /no-cors/delete/{id} 删除
     * @apiGroup api-不支持跨域
     * @apiParam {String} id 主键
     */
    @Operation(summary = "删除")
    @ApiImplicitParam(name = "id", value = "对象id", required = true, paramType = "path")
    @DeleteMapping("/delete/{id:\\d+}")
    public JsonResult<?> delete(@PathVariable Long id)
    {
        return JsonResult.success();
    }
    
    /**
     * Get查询
     */
    @Operation(summary = "Get查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "对象id", required = true, paramType = "query")})
    @GetMapping("/query")
    public JsonResult<DataEntity> query(@RequestParam Long id)
    {
        DataEntity dataEntity = new DataEntity();
        dataEntity.setId(1L).setAge(18).setSex("F").setSalary(10000.00F).setGame1(99.999D).setGame2(new BigDecimal("65536.2356655"));
        return JsonResult.success(dataEntity);
    }
    
    /**
     * Path查询
     */
    @Operation(summary = "Path查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "对象id", required = true, paramType = "path")})
    @GetMapping("/query/{id:\\d+}")
    public JsonResult<?> query1(@PathVariable Long id)
    {
        return JsonResult.success();
    }
    
    /**
     * 列表查询
     */
    @Operation(summary = "列表查询")
    @GetMapping("/queryList")
    public List<DataEntity> queryList()
    {
        List<DataEntity> list = new ArrayList<>();
        LongStream.range(0, 5).forEach(i -> list.add(new DataEntity().setId(i).setName("jack" + i)));
        return list;
    }
}
